require("../util.js");
require("../component/components.js");
require("../../jsMt/util.js");
var userInfo=JSON.parse(sessionStorage.getItem("userInfo"));
// if(!userInfo){
//     location.href="/loginIndex"
// }
var vm = new Vue({
    el:"#userGoodsInfo",
    data:{
        uid:"",
        productId:"",
        productById:{},
        cartCountNum:"0",//购物车数量
        colorName:"",//颜色
        colorId:"",
        specName:"",//规格
        // specId:"",
        integral:"",//价格
        imgSrc:"",
        skuId:"",
        stocks:"",  //库存
        user:{},
        isClassify:false,
        number:1,
        cityName:"",
        isTop:false,
        isShare:false,
        num:""
    },
    mounted:function(){
        this.searchObject = this.getQueryStringArgs();
        this.productId=this.searchObject&&this.searchObject.productId;
        this.uid=this.searchObject&&this.searchObject.uid;
        if(this.searchObject&&this.searchObject.number){
            this.num=this.searchObject&&this.searchObject.number;
        }else{
            this.cartCount();
        }
        this.getProductById();
    },
    methods:{
        //后退
        getBack:function(){
            history.go(-1);
        },
        bgimg:function(url){
            url = url?url:"";
            return {
                "background-image":'url("'+url+'"),url("")'
            }
        },
        /*获取查询字符对象*/
        getQueryStringArgs: function () {
            //取得查询字符串并去掉开头的问号
            var qs = (location.search.length > 0 ? location.search.substring(1) : ""),
                args = {},
                items = qs.length ? qs.split("&") : [],
                item = null,
                name = null,
                value = null,
                len = items.length;
            for (var i = 0; i < len; i++) {
                item = items[i].split("=");
                name = item[0];
                value = item[1];
                if (name.length) {
                    args[name] = value;
                }
            }
            return args;
        },
        //显示弹框
        isDisplay:function(){
            this.isClassify=!this.isClassify;
        },
        //数量减
        getReduce:function(){
            if(this.number==0){
                return false;
            }else{
                this.number--;
            }
        },
        //数量加
        getAdd:function(){
            if(this.number<this.stocks){
                this.number++;
            }
        },
        //选择及时改变
        getInfo:function(item){
            var that=this;
            console.log(item)
            that.specName=item.model;
            that.skuId=item.skuId;
            that.stocks=item.num;
            that.integral=item.price
        },
        //购物车数量
        cartCount:function(){
            var that=this;
            this.ajax({url:"/purchase/cartCount",data:{loginUid:userInfo.id},dataType:"json",method:"post"},function(b){
                if(b.json.code==0){
                    that.cartCountNum=b.json.data;
                }else{
                    vm.$refs.prompt.toast("获取购物车数量失败！")
                }
            });
        },
        //个人商品详情
        getProductById:function(){
            var that=this;
            var data={
                productId:that.productId
                // loginUid:userInfo.id
            };
            this.ajax({url:"/user/getMediaProductById",data:data,dataType:"json",method:"post"},function(b){
                if(b.json.code==0&&b.json.data){
                    that.productById=b.json.data;
                    that.user=that.productById.user;
                    that.integral=that.productById.integral;
                    if(that.productById.images&&that.productById.images.length>0){
                        that.imgSrc=that.productById.images[0].imgSrc;
                    }

                    //that.comment=that.productById.comment;
                }else{
                    vm.$refs.prompt.toast("获取商品详情失败！");
                }
            });
        },
        //加入购物车
        addCart:function(){
            var that=this;
            if(!userInfo){
                location.href="/loginIndex?number=16&productId="+this.productId;
                return false;
            }
            if(!that.skuId){
                that.isDisplay();
                // vm.$refs.prompt.toast("请选择规格！");
                return;
            }
            var data={
                loginUid:that.uid||userInfo.id,
                mediaUserId:that.productById.mediaUserId,
                productId:that.productId,
                skuId:that.skuId,
                number:that.number
            };
            console.log(data)
            this.ajax({url:"/purchase/addCart",data:data,dataType:"json",method:"post"},function(b){
                if(b.json.code==0&&b.json.data){
                    that.cartCountNum=b.json.data.cartCount;
                    // this.isClassify=false;
                    that.isDisplay();
                    vm.$refs.prompt.toast("加入购物车成功！");
                }else{
                    vm.$refs.prompt.toast("加入购物车失败！");
                }
            });
        },
        getNext:function(type){
            if(type==1){
                location.href="/purchase/shoppingCart"
            }else if(type==2){
                if(this.user.id!=userInfo.chatId){
                    location.href="/friend?type=1&circleId="+this.user.id+"&uid="+this.uid||userInfo.id;
                }else{
                    vm.$refs.prompt.toast("不能和自己聊天！")
                }
            }
        },
        getShare:function(){
            this.isShare=!this.isShare;
        },
        getChoice:function(num){
            if(num==1){
                location.href="/friend/choiceList?number=10&postId="+this.productId;
            }else{
                location.href="/friend/forward?number=10&postId="+this.productId;
            }
        }
    }
    ,watch:{
        productById:function(){
            this.$nextTick(function(){
                var swiper1 = new Swiper(".purchase-slider .swiper-container",{
                    spaceBetween: 30,
                    centeredSlides: true,
                    // autoplay: {
                    //     delay: 2500,
                    //     disableOnInteraction: false
                    // },
                    pagination: {
                        el: '.swiper-pagination',
                        type: 'fraction'
                    },
                    lazy: {
                        loadPrevNext: true,
                        loadPrevNextAmount: 2
                    },
                    observer: true,//修改swiper自己或子元素时，自动初始化swiper
                    observeParents: true//修改swiper的父元素时，自动初始化swiper
                });
            });
        }
    }
});
var map = new AMap.Map('iCenter', {
    resizeEnable: true
});
showCityInfo();
//获取用户所在城市信息
function showCityInfo() {
    //实例化城市查询类
    var citysearch = new AMap.CitySearch();
    //自动获取用户IP，返回当前城市
    citysearch.getLocalCity(function(status, result) {
        console.log(result);
        if (status === 'complete' && result.info === 'OK') {
            if (result && result.city) {
                vm.cityName=result.city;
            }
        } else {
            vm.cityName=result.info;
        }
    });
}
//文档高度
function getDocumentTop() {
    var scrollTop = 0, bodyScrollTop = 0, documentScrollTop = 0;
    if (document.body) {
        bodyScrollTop = document.body.scrollTop;
    }
    if (document.documentElement) {
        documentScrollTop = document.documentElement.scrollTop;
    }
    scrollTop = (bodyScrollTop - documentScrollTop > 0) ? bodyScrollTop : documentScrollTop;
    return scrollTop;
}
window.onscroll=function(){
    if(getDocumentTop()>40){
        vm.isTop=true;
    }else{
        vm.isTop=false;
    }
};